<?php

session_start();
//error_reporting(0);
if(!isset($_SESSION['userId']))
{
	header("location:index.php");
}
include("../class/config.php");
include("../class/cls_admin.php");

if(isset($_POST['send'])=="add" && $_POST!=NULL)
{
	
	$fname=$_POST['user_fname']; 
	$lname=$_POST['user_lname'];
	$email=$_POST['user_email'];
	$password=md5($_POST['user_password']);
	$cpassword=md5($_POST['user_cpassword']);
		
	if($password==$cpassword){
		if(($_GET['action'])=='add'){
			if(!$_POST['user_fname']=="" &&!$_POST['user_lname']=="" &&!$_POST['user_email']=="" &&!$_POST['user_password']==""){
			$result= $objAdmin -> insertAdmin($fname, $lname, $email, $password);
			}
		} 
		else if(($_GET['action'])=='edit'){
		
			$id= $_GET['editId'];
			if(!$_POST['user_fname']=="" &&!$_POST['user_lname']=="" &&!$_POST['user_email']=="" &&!$_POST['user_password']==""){
				$result= $objAdmin -> updateAdmin($id, $fname, $lname, $email, $password);
				if(result){
					header("location:adminuser.php");
				}
			}
		}
		if(isset($result)){
			$success= "<span class='successmsg'>Process completed</span>";
		}
		if(!isset($result)){
			$success= "<span class='errormsg'>Process failed !!</span>";
		}
	}
	else $success= "<span class='errormsg'>Password didn't matched !!</span>";
}

	if(isset($_GET['deleteId']))
	{
		$adminId=$_GET['deleteId'];
		$objAdmin->deleteAdmin($adminId);
		}
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Banner</title>
<link rel="stylesheet" href="../css/layout.css" />
</head>
<body>

	<div class="mainWrapper">
     <?php include("../includes/adminHeader.php");?>
    
    
    <div class="bodyContent">
    <h1>Admin User</h1>
    <div class="innerBody">
    <?php if(!isset($_GET['action']))
	{?>
    <strong>Admin User Listing</strong>  | <a href="?action=add">Add admin</a>
     
	
      <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
        <tr>
          <th>Sn</th>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Email </th>
          <th>Action</th>
        </tr>
		<?php $resAdmin = $objAdmin->selectAdmin();
		$i=1;
		
		while($recAdmin=mysqli_fetch_array($resAdmin))
		{
		?>
        <tr>
          <td><?php echo $i++;?></td>
          <td><?php echo $recAdmin['user_fname'];?></td>
          <td><?php echo $recAdmin['user_lname'];?></td>
          <td><?php echo $recAdmin['user_email'];?></td>

		  		  
          <td><a href="?action=edit&editId=<?php echo $recAdmin['user_id'];?>">Edit</a> | 
          		<a href="?deleteId=<?php echo $recAdmin['user_id'];?>"
          			 onclick="javascript:return confirm('Are You Sure You Want to Delete this Record?')">Delete</a>
			<a href="?editId=<?php echo $recAdmin['user_id']?>"></a></td>
        </tr>
        <?php } /// end of while($recContact=mysqli_fetch_array($resContact))
?>
       </table>
       
     <?php }
	  else if(isset($_GET['action']) && $_GET['action']=='add'or (isset($_GET['action']) && $_GET['action']=='edit') )
	 {
	 
	 ?>
      <strong><?php echo $_GET['action']=="add"?"Add Faq":"Edit Faq" ?></strong>
      <?php 
	  if(isset($success))
      {
      echo $success;
      } ?>
      <form id="form1" name="form1" method="post" action="">
        <table width="100%" border="0" class="table" cellpadding="1" cellspacing="1" >
        	
        	<?php
        	 
        	 if(($_GET['action'])=='edit'){
        	 	
			 $adminId=$_GET['editId'];
			$res= $objAdmin->getAdminById($adminId);
        	$result=mysqli_fetch_array($res);
			
        	 }?>
          <tr>
            <td width="30%">User First Name *</td>
            <td><input type="text" name="user_fname" id="user_fname" class="textBox"
            	 value="<?php if(($_GET['action'])=='edit')echo $result["user_fname"];?>"/>
            	 <?php if(isset($_POST['user_fname']) &&$_POST['user_fname']==''){
            	 	echo "<span class= 'errormsg'>Empty first name !!</span>";
            	 }?></td>
          </tr>
          <tr>
            <td width="30%">User Last Name *</td>
            <td><input type="text" name="user_lname" id="user_lname" class="textBox"
            	 value="<?php if(($_GET['action'])=='edit')echo $result["user_lname"];?>"/>
            	 <?php if(isset($_POST['user_lname']) &&$_POST['user_lname']==''){
            	 	echo "<span class= 'errormsg'>Empty last name !!</span>";
            	 }?></td>
          </tr>
          <tr>
             <td width="30%">User Email *</td>
            <td><input type="text" name="user_email" id="user_email"
            	value="<?php if(($_GET['action'])=='edit')echo $result["user_email"];?>"/>
            	<?php if(isset($_POST['user_email']) &&$_POST['user_email']==''){
            	 	echo "<span class= 'errormsg'>Empty email  !!</span>";
            	 }?></td>
          </tr>
          <tr>
             <td width="30%">User Password*</td>
            <td><input type="password" name="user_password" id="user_password"
            	value=""/>
            	<?php if(isset($_POST['user_password']) &&$_POST['user_password']==''){
            	 	echo "<span class= 'errormsg'>Empty password !!</span>";
            	 }?></td>
          </tr>
            <tr>
             <td width="30%">User Conform Password*</td>
            <td><input type="password" name="user_cpassword" id="user_cpassword"
            	value=""/>
            	<?php if(isset($_POST['user_cpassword']) &&$_POST['user_cpassword']==''){
            	 	echo "<span class= 'errormsg'>Empty conform password  !!</span>";
            	 }?></td>
          </tr>
        	<td>&nbsp;</td>
            <td><input type="submit" name="send" id="add" value="<?php echo $_GET['action']=="add"?"  Add  ":"  Edit  "?> " />
            	<input type="button" name="cancel" value="cancel" onclick="javascript:location.href='adminuser.php'" /></td>
          </tr>
         <?php 
         			
		}?>
        </form>

           </div>
    
   </div>
    
    
    <?php //include("../includes/adminFooter.php");?>
     
 </div>

</body>
</html>